package com.summer.service.impl;

import com.summer.entity.OrderRepayment;
import com.summer.entity.OrderRepaymentDetail;
import com.summer.mapper.OrderRepaymentDetailDAO;
import com.summer.mapper.OrderRepaymentMapper;
import com.summer.service.IOrderRepaymentService;
import com.summer.util.DateUtil;
import com.summer.variable.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Desc:
 * Created by tl on 2019/1/3
 */
@Service
public class OrderRepaymentService implements IOrderRepaymentService {
    private static Logger log = LoggerFactory.getLogger(OrderRepaymentService.class);
    @Resource
    private OrderRepaymentMapper orderRepaymentMapper;
    @Resource
    private OrderRepaymentDetailDAO orderRepaymentDetailDao;

    @Override
    @Transactional
    public void payOffline(OrderRepayment orderRepayment, Object payTimeObj, String remark, String thirdOrderNo,
                           Object payTypeObj, int paidAmount) {
        Integer id = orderRepayment.getId();
        Date now = new Date();
        // TODO CURRENTUSER
//        newOrderDetail.setOperatorUserId();
        OrderRepaymentDetail newOrderDetail = new OrderRepaymentDetail(orderRepayment.getBorrowId(), id,
                orderRepayment.getUserId(), paidAmount, remark, thirdOrderNo, 0, now, "", now, now, 0,
                Constant.REPAYMENTDETAIL_STATUS_PAID, Constant.REPAYMENTDETAIL_TYPE_NORMAL,
                payTypeObj == null ? Constant.PAY_TYPE_UNKNOWN :
                        Byte.parseByte(payTypeObj.toString()));
        if (DateUtil.daysBetween(orderRepayment.getRepaymentTime(), now) > 0) {
            newOrderDetail.setOverdue(Constant.REPAYMENTDETAIL_TYPE_OVERDUE);
        } else if (DateUtil.daysBetween(orderRepayment.getRepaymentTime(), now) < 0) {
            newOrderDetail.setOverdue(Constant.REPAYMENTDETAIL_TYPE_FORWARD);
        }
        if (null != payTimeObj) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date date = sdf.parse(payTimeObj.toString());
                newOrderDetail.setPayTime(date);
            } catch (ParseException e) {
                log.error("OrderRepaymentService payOffline dateParseError id=" + id);
            }
        }
        OrderRepayment newOrder = new OrderRepayment();

        newOrder.setId(id);
        newOrder.setPaidAmount(orderRepayment.getPaidAmount() * 100 + paidAmount);
        newOrder.setPayType(newOrderDetail.getPayType());
        if (paidAmount == (orderRepayment.getRepaymentAmount() - orderRepayment.getPaidAmount()) * 100) {
            newOrder.setStatus(Constant.REPAYMENT_STATUS_PAID);
            newOrder.setPaidTime(now);
            if (Constant.REPAYMENTDETAIL_TYPE_FORWARD == newOrderDetail.getOverdue()) {
                newOrder.setPaidForward(Constant.REPAYMENT_TYPE_FORWARD);
            }
        } else {
            newOrder.setStatus(Constant.REPAYMENT_STATUS_PART_PAID);
        }
        orderRepaymentMapper.updateByPrimaryKeySelective(newOrder);
        orderRepaymentDetailDao.insertSelective(newOrderDetail);
    }
}
